package com.wing.bill.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.controller.admin.ReportUtil;
import com.wing.bill.dao.CabinetFormDao;
import com.wing.bill.dao.UnreturnReportDao;
import com.wing.cabinet.model.response.UseInventoryProductVO;
import com.wing.cabinet.model.response.UseInventoryUserVO;
import com.wing.cabinet.service.UseInventoryUserService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import com.wing.common.utils.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UnreturnReportServiceImpl implements UnreturnReportService{
    @Autowired
    private UnreturnReportDao unreturnReportDao;
    @Autowired
    private UseInventoryUserService useInventoryUserService;
    @Override
    public JsonResult list(String type, String startTime, String endTime, String mainCabCode, String code,Integer click) {
        HashMap map = new HashMap(){{put("title","未归还物料总数量(支)");put("sign","1");}};
        HashMap map1 = new HashMap(){{put("title","未归还物料金额(元)");put("sign","2");}};
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==0){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            map.putAll(unreturnReportDao.listNum(start,end,mainCabCode,code));
            map1.putAll(unreturnReportDao.listMoney(start,end,mainCabCode,code));
        }
        if (type.equals("0")&&click==0){
            map.putAll(unreturnReportDao.listNum(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            map1.putAll(unreturnReportDao.listMoney(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
        }else if (type.equals("1")&&click==0){
            map.putAll(unreturnReportDao.listNum(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            map1.putAll(unreturnReportDao.listMoney(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
        }else if (type.equals("2")&&click==0){
            map.putAll(unreturnReportDao.listNum(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            map1.putAll(unreturnReportDao.listMoney(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
        }
        if (StringUtils.isNotBlank(type)&&click==1){
            if (type.equals("0")){
                return JsonResult.success("查询成功",unreturnReportDao.listNumClick(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            }else if (type.equals("1")){
                return JsonResult.success("查询成功",unreturnReportDao.listNumClick(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            }else if (type.equals("2")){
                return JsonResult.success("查询成功",unreturnReportDao.listNumClick(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            }
        }
        if (StringUtils.isNotBlank(type)&&click==2){
            if (type.equals("0")){
                return JsonResult.success("查询成功",unreturnReportDao.listMoneyClick(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            }else if (type.equals("1")){
                return JsonResult.success("查询成功",unreturnReportDao.listMoneyClick(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            }else if (type.equals("2")){
                return JsonResult.success("查询成功",unreturnReportDao.listMoneyClick(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            }
        }
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==1){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            return JsonResult.success("查询成功",unreturnReportDao.listNumClick(start,end,mainCabCode,code));
        }
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)&&click==2){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            return JsonResult.success("查询成功",unreturnReportDao.listMoneyClick(start,end,mainCabCode,code));
        }
        List<HashMap> list = new ArrayList();
        list.add(map);
        list.add(map1);
        return JsonResult.success("查询成功",list);
    }

    @Override
    public JsonResult midFirstChart(String type, String startTime, String endTime, String mainCabCode, String code, String param, String other) {
        HashMap hm1 = new HashMap();
        if (other.equals("person")){
            hm1.put("title","物料未归还(按人员)");
        }else if(other.equals("device")){
            hm1.put("title","物料未归还(按设备)");
        }else if(other.equals("skuCode")){
            hm1.put("title","物料未归还物料占比");
        }else if(other.equals("order")){
            hm1.put("title","物料未归还(按工单)");
        }
        List<HashMap> list1 = new ArrayList<>();
        List list = new ArrayList();
        HashMap map = new HashMap();
        List list2 = new ArrayList();
        HashMap map2 = new HashMap();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            list1.addAll(unreturnReportDao.midFirstChart(start,end,param,code,mainCabCode,other));
            if(other.equals("skuCode")){
                map2.putAll(unreturnReportDao.midFirstChartOne(start,end,code,mainCabCode));
                map2.putAll(unreturnReportDao.midFirstChartTwo(start,end,code,mainCabCode));
            }
            if(other.equals("person")){
                map.putAll(unreturnReportDao.midFirstChartThree(start,end,code,mainCabCode,param));
            }
        }
        if (type.equals("0")){
            list1.addAll(unreturnReportDao.midFirstChart(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),param,code, mainCabCode,other));
            if(other.equals("skuCode")){
                map2.putAll(unreturnReportDao.midFirstChartOne(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code, mainCabCode));
                map2.putAll(unreturnReportDao.midFirstChartTwo(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code, mainCabCode));
            }
            if(other.equals("person")){
                map.putAll(unreturnReportDao.midFirstChartThree(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),code,mainCabCode,param));
            }
        }else if (type.equals("1")){
            list1.addAll(unreturnReportDao.midFirstChart(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),param,code, mainCabCode,other));
            if(other.equals("skuCode")){
                map2.putAll(unreturnReportDao.midFirstChartOne(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code, mainCabCode));
                map2.putAll(unreturnReportDao.midFirstChartTwo(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code, mainCabCode));
            }
            if(other.equals("person")){
                map.putAll(unreturnReportDao.midFirstChartThree(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),code,mainCabCode,param));
            }
        }else if (type.equals("2")){
            list1.addAll(unreturnReportDao.midFirstChart(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),param,code, mainCabCode,other));
            if(other.equals("skuCode")){
                map2.putAll(unreturnReportDao.midFirstChartOne(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code, mainCabCode));
                map2.putAll(unreturnReportDao.midFirstChartTwo(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code, mainCabCode));
            }
            if(other.equals("person")){
                map.putAll(unreturnReportDao.midFirstChartThree(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),code,mainCabCode,param));
            }
        }
        if(other.equals("skuCode")){
            list2.add(map2);
            hm1.put("totalSum",list2);
        }
        if(other.equals("person")){
            list.add(map);
            hm1.put("totalSumOne",list);
        }
        hm1.put("data",list1);
        return JsonResult.success("查询成功",hm1);
    }

    @Override
    public JsonResult midSecondChart(String type, String startTime, String endTime, String mainCabCode, String code) {
        HashMap hm1 = new HashMap();
        hm1.put("title","物料归还对比图");
        HashMap hm2 = new HashMap();
        hm2.put("title","未归还物料");
        HashMap hm3 = new HashMap();
        hm3.put("title","已归还物料");
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            hm2.putAll(unreturnReportDao.midSecondChartOne(start,end,mainCabCode,code));
            hm3.putAll(unreturnReportDao.midSecondChartTwo(start,end,mainCabCode,code));
        }
        if (type.equals("0")){
            hm2.putAll(unreturnReportDao.midSecondChartOne(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
            hm3.putAll(unreturnReportDao.midSecondChartTwo(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),mainCabCode,code));
        }else if (type.equals("1")){
            hm2.putAll(unreturnReportDao.midSecondChartOne(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
            hm3.putAll(unreturnReportDao.midSecondChartTwo(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),mainCabCode,code));
        }else if (type.equals("2")){
            hm2.putAll(unreturnReportDao.midSecondChartOne(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
            hm3.putAll(unreturnReportDao.midSecondChartTwo(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),mainCabCode,code));
        }
        List<HashMap> list2 = new ArrayList<>();
        list2.add(hm2);
        list2.add(hm3);
        hm1.put("data",list2);
        List list = new ArrayList();
        HashMap map = new HashMap();
        map.put("value",hm3.get("numRatio"));
        list.add(map);
        hm1.put("totalSumOne",list);
        return JsonResult.success("查询成功",hm1);
    }

    @Override
    public PageResult data(String type, String startTime, String endTime, Integer page, Integer limit, String sort, String order,String mainCabCode) {
        if(StringUtils.isNotBlank(type)){
            if (type.equals("0")){
                Page<UseInventoryProductVO> pg = new Page<>(page, limit);
                Map<String, Object> queryParam = new HashMap<>();
                queryParam.put("sort", StrUtil.toUnderlineCase(sort));
                queryParam.put("order",order);
                queryParam.put("startCreateTime",ReportUtil.dayUtil().get(0).toString());
                queryParam.put("endCreateTime",ReportUtil.dayUtil().get(1).toString());
                queryParam.put("mainCabCode",mainCabCode);
                Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, queryParam);
                return new PageResult<>(list.getRecords(), list.getTotal());
            }else if (type.equals("1")){
                Page<UseInventoryProductVO> pg = new Page<>(page, limit);
                Map queryParam=new HashMap();
                queryParam.put("sort",StrUtil.toUnderlineCase(sort));
                queryParam.put("order",order);
                queryParam.put("startCreateTime",ReportUtil.weekUtil().get(0).toString());
                queryParam.put("endCreateTime",ReportUtil.weekUtil().get(1).toString());
                queryParam.put("mainCabCode",mainCabCode);
                Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, queryParam);
                return new PageResult<>(list.getRecords(), list.getTotal());
            }else{
                Page<UseInventoryProductVO> pg = new Page<>(page, limit);
                Map queryParam=new HashMap();
                queryParam.put("sort",StrUtil.toUnderlineCase(sort));
                queryParam.put("order",order);
                queryParam.put("startCreateTime",ReportUtil.dateUtil().get(0).toString());
                queryParam.put("endCreateTime",ReportUtil.dateUtil().get(1).toString());
                queryParam.put("mainCabCode",mainCabCode);
                Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, queryParam);
                return new PageResult<>(list.getRecords(), list.getTotal());
            }
        }
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            Page<UseInventoryProductVO> pg = new Page<>(page, limit);
            Map queryParam=new HashMap();
            queryParam.put("sort",StrUtil.toUnderlineCase(sort));
            queryParam.put("order",order);
            queryParam.put("startCreateTime",start.toString());
            queryParam.put("endCreateTime",end.toString());
            queryParam.put("mainCabCode",mainCabCode);
            Page<UseInventoryUserVO> list = useInventoryUserService.pageList4app(pg, queryParam);
            return new PageResult<>(list.getRecords(), list.getTotal());
        }
        return null;
    }
}
